#include "dwgstar.h"
#include <QPainter>
#include <QtMath>

DwgStar::DwgStar(QObject *parent)
    : DwgObject(parent)
{
}


void DwgStar::Draw(QPainter *painter)
{
    //=========抑制线宽变化========//
    QPen pen;
    pen.setCosmetic(true);       // 允许线宽缩放
    pen.setWidth(1);             // 使用逻辑线宽
    painter->setPen(pen);
    //==========选择+删除=========//
    if(mDeleteed) return;
    if(mSelected)
    {
        painter->setPen(Qt::red);
    }

    // 计算五角星五个顶点坐标（基于外接圆）
    QPointF points[5];
    const double angleStep = 72.0;  // 每顶点间隔72度（360/5）
    for (int i = 0; i < 5; ++i) {
        double angle = (i * angleStep - 90) * M_PI / 180;  // 转换为弧度，起始点位于正上方
        double x = mCenter.x() + mRadius * cos(angle);
        double y = mCenter.y() + mRadius * sin(angle);
        points[i] = QPointF(static_cast<int>(x), static_cast<int>(y));
    }

    // 绘制五角星路径（连接隔两顶点）
    painter->drawLine(points[0], points[2]);
    painter->drawLine(points[2], points[4]);
    painter->drawLine(points[4], points[1]);
    painter->drawLine(points[1], points[3]);
    painter->drawLine(points[3], points[0]);  // 闭合路径形成完整五角星

    painter->setPen(Qt::black);
}

void DwgStar::SetData(QPointF pt1, QPointF pt2)
{
        mCenter = pt1;
        mRadius = qSqrt((pt2.x()-pt1.x())*(pt2.x()-pt1.x())+
                                   (pt2.y()-pt1.y())*(pt2.y()-pt1.y()));
}

//==选择+删除==//
void DwgStar::Select(QPointF pt)
{
    qreal dist = sqrt((pt.x()-mCenter.x())*(pt.x()-mCenter.x())
                      +(pt.y()-mCenter.y())*(pt.y()-mCenter.y()));
    if(fabs(dist-mRadius) < 3)
        mSelected = true;
}
